
rm(list = ls())

library('data.table')
library('stargazer')
library('ggplot2')


#########################################################################################################################

################################# 
# Prepare Dataset for Product 1 #
#################################

# Import and Clean Data 

i_k = 1
data_name <- sprintf("price_index/product_specific/diff_full_adj_table_%s_k.csv", i_k)
data = read.table(data_name, header =TRUE, sep=",")
data <- as.data.table(data)

# Remove observations if |beta| < 0.6 or if the Home country's mc could not be inferred (demand not sufficiently elastic)
data <- data[(abs(beta_price_80) > 0.6) | (abs(beta_price_20) > 0.6)]
data <- data[domestic_producer == 1]
data <- data[mc_flag_true == 1]
data <- data[mc_flag_counter == 1]
data <- data[mc_flag_f_opt == 1]

data <- data[ , P_true_sorted_10 := NULL]
data <- data[ , P_true_sorted_90 := NULL]
data <- data[ , P_counter_sorted_10 := NULL]
data <- data[ , P_counter_sorted_90 := NULL]

data <- data[ , product := i_k]   



I_K = 4092

for (i_k in 2:I_K) {
    
  print(i_k)  
  
  # Import and Clean Data  
  data_name <- sprintf("price_index/product_specific/diff_full_adj_table_%s_k.csv", i_k)
  if (file.exists(data_name) == T) {
  data_temp = read.table(data_name, header =TRUE, sep=",")
  data_temp <- as.data.table(data_temp)
  
  # Remove observations if |beta| < 0.6 or if the Home country's mc could not be inferred (demand not sufficiently elastic)
  data_temp <- data_temp[(abs(beta_price_80) > 0.6) | (abs(beta_price_20) > 0.6)]
  data_temp <- data_temp[domestic_producer == 1]
  data_temp <- data_temp[mc_flag_true == 1]
  data_temp <- data_temp[mc_flag_counter == 1]
  data_temp <- data_temp[mc_flag_f_opt == 1]
  
  data_temp <- data_temp[ , P_true_sorted_10 := NULL]
  data_temp <- data_temp[ , P_true_sorted_90 := NULL]
  data_temp <- data_temp[ , P_counter_sorted_10 := NULL]
  data_temp <- data_temp[ , P_counter_sorted_90 := NULL]
    
  data_temp <- data_temp[ , product := i_k]  
  
  data = rbind(data, data_temp)
  }
}  


save.image('data_PI_full_adj.RData')

